﻿using Microsoft.Practices.Unity;

using SimpleForum.Classes.Web;
using SimpleForum.Classes.Core;
using SimpleForum.Data.Models;
using SimpleForum.Data.Services;
using SimpleForum.Data.Repositories;

namespace SimpleForum
{
    public class MvcApplication : BootStrapper
    {
        public static IUnityContainer unityContainer;
        private CSLExportProvider exportProvider;

        protected override void PreCompose()
        {
            unityContainer = new UnityContainer();
            var locator = new UnityServiceLocator(unityContainer);

            Microsoft.Practices.ServiceLocation.ServiceLocator.SetLocatorProvider(() => locator);

            exportProvider = new CSLExportProvider(locator);
            unityContainer.AddExtension(new CSLExportProviderExtension(exportProvider));

            RegisterTypes();
        }

        protected void RegisterTypes()
        {
            unityContainer.RegisterType<DatabaseContextContainer>(new InjectionConstructor());
            unityContainer.RegisterType(typeof(IRepository<>), typeof(Repository<>));
            
            // find a better way of registering all of our services
            unityContainer.RegisterType<IUserService, UserService>();
            unityContainer.RegisterType<ICategoryService, CategoryService>();
            unityContainer.RegisterType<IForumService, ForumService>();
            unityContainer.RegisterType<IPostService, PostService>();
            unityContainer.RegisterType<ITagService, TagService>();
            unityContainer.RegisterType<IThreadService, ThreadService>();
        }

        protected override Composer CreateComposer()
        {
            var composer = base.CreateComposer();
            composer.AddExportProvider(exportProvider);

            return composer;
        }
    }
}